﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.ComponentModel;
using System.Management.Automation;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Configuration.Install;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.IO;
using PowerShell.Host.PsTFS.CdmLet.PsObject;
using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace PowerShell.Host.PsTFS.CdmLet
{
    /// <summary>
    /// Ajout d'un workitem
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "WorkItem")]
    public class Add_WI : Cmdlet
    {
        public Add_WI()
        {
            if (TFSProvider.s_TFSDriveInfo != null)
            {
                this.TFS = TFSProvider.s_TFSDriveInfo.TFS;
                this.ProjectName = TFSProvider.s_TFSDriveInfo.ProjectName;
            }
            this.Type = string.Empty;
            this.Title = string.Empty;
            this.Param = null;
        }
        protected override void ProcessRecord()
        {
            if (TFS == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server TFS!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
                return;
            }
            if (ProjectName == null)
            {
                WriteError(new ErrorRecord(new ArgumentException("No server ProjectName!!!"), "PsTFS", ErrorCategory.ObjectNotFound, TFS));
                return;
            }
                WorkItem createWi = PsWorkitem.CreateWI(this.TFS, this.ProjectName, this.Type, this.Title, this.Param);
            //Information
             if (createWi != null)
             {
                 PsLog.AddLog(null, "Successfully created workitem " + this.Title);
             }
             else
             {
                 PsLog.AddLog("Failed workitem " + this.Title);
             }

            //Renvois les informations
            WriteObject(createWi);
        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            PsLog.AddLog(null, "Add-Wi is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server 2008
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS { get; set; }

        /// <summary>
        /// Nom du label
        /// </summary>
        [Parameter(Position = 1)]
        public string ProjectName { get; set; }

        /// <summary>
        /// Type du workitem a créer
        /// </summary>
        [Parameter(Position = 2, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Type { get; set; }

        /// <summary>
        /// Titre du workitem
        /// </summary>
        [Parameter(Position = 3, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Title { get; set; }

        /// <summary>
        /// Elements à ajouter au workitem
        /// </summary>
        [Parameter(Position = 4)]
        [ValidateNotNullOrEmpty]
        public string[] Param { get; set; }
    }
}
